﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.HtmlControls;
using NextCMS.Models;

public partial class WebAdmin_ManageMasterPages : WebadminBasePage
{
    int Id = 0;
    public string MasterPageCss = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.Form["hidMasterPageId"]), out Id);
        if (!IsPostBack)
        {
            if (Id > 0)
            {
                string command =Convert.ToString(Request.Form["hidCommand"]);
                if (command == "Save")
                {
                    TemplatesHelper cc = new TemplatesHelper();
                    TemplateModel mp = cc.GetById(Id);
                    mp.Name = Convert.ToString(Request.Form["txtMasterPageName"]);
                    if (Request.Files.Count > 0  && Request.Files[0].FileName != "")
                    {

                        string fileName = Guid.NewGuid() + Path.GetExtension(Request.Files[0].FileName);
                        string filePath = Server.MapPath("~/Resources/Uploads/Images/") + fileName;
                        mp.TemplateImageName = fileName;
                        ResizeImage(filePath, 120, 90, Request.Files[0].InputStream);
                    }
                    cc.Edit(mp);
                }
                else if (command == "Delete")
                {
                    DeleteTemplate();
                }

                //txtContent.Text = mp.Content;
                //txtMasterPageName.Text = mp.MasterPageName;
                //MasterPageCss = GetAllCss(mp.MasterPageName);
                
                //btnDelete.Visible = true;
            }
            else
            {
                
                //btnDelete.Visible = false;
            }
            BindFrames();
        }

        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-1.7.js", ResolveClientUrl("~/Resources/Javascript/jquery-1.7.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-ui-1.8.4.custom.min.js", ResolveClientUrl("~/FileManager/js/jquery-ui-1.8.4.custom.min.js"));
        AddCssLink("FileManager/css/ui-lightness/jquery-ui-1.8.4.custom.css");
    }
    void BindFrames()
    {
        TemplatesHelper mpc = new TemplatesHelper();
        RPTFrames.DataSource = mpc.GetAllBySiteId(SiteId);
        RPTFrames.DataBind();
    }
    
    private void DeleteTemplate()
    {
        PagesHelper cc = new PagesHelper();
        TemplatesHelper mpc = new TemplatesHelper();
        
        IEnumerable<PageModel> lstPageModel = cc.GetByMasterPageId(Id).ToList();

        
        foreach (PageModel pm in lstPageModel)
        {
            PageModulesHelper pmc = new PageModulesHelper();
            IEnumerable<PageModuleModel> lstPagModuleeModel = pmc.GetByPageId(pm.Id).ToList();
            foreach (PageModuleModel pmm in lstPagModuleeModel)
                pmc.Delete(pmm.Id);
            cc.Delete(pm.Id);
        }
        mpc.Delete(Id);
    }
    protected void RPTFrames_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlImage imgTemplate = e.Item.FindControl("imgTemplate") as HtmlImage;
            if (imgTemplate != null)
            {
                string fileName=Convert.ToString(DataBinder.Eval(e.Item.DataItem, "TemplateImageName"));
                if(!String.IsNullOrEmpty(fileName))
                    imgTemplate.Src = "~/Resources/Uploads/Images/" + fileName;
            }
        }
    }
}